package com.sky.chapter3.threadLocal;

import java.util.concurrent.TimeUnit;

/**
 * @author : wushikai
 * <p>
 * date : 2022-07-06
 */
class MovieTicket1 {
    private int number = 50;

    synchronized void saleTicket() {
        if (number > 0) {
            System.out.println(Thread.currentThread().getName() + "\t" + "号售票员卖出第： " + (number--));
        } else {
            System.out.println("--------卖完了");
        }
    }
}

/**
 * 三个售票员卖完50张票务，总量完成即可，吃大锅饭，售票员每个月固定月薪
 */
public class ThreadLocalDemo1 {

    public static void main(String[] args) {
        MovieTicket1 movieTicket1 = new MovieTicket1();

        for (int i = 1; i <= 3; i++) {
            new Thread(() -> {
                for (int j = 0; j < 20; j++) {
                    movieTicket1.saleTicket();
                    try {
                        TimeUnit.MILLISECONDS.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, String.valueOf(i)).start();
        }
    }
}

